//
//  ContactViewController.m
//  cliqset
//
//  Created by David Hinson on 2/9/09.
//  Copyright 2009 Sumner Systems Management, Inc.. All rights reserved.
//

#import "FavoritesViewController.h"
#import "cliqsetAppDelegate.h"
#import "Contact.h"
#import "BoxView.h"

@implementation FavoritesViewController

@synthesize myNavBar;
@synthesize favoritesTable;
@synthesize hoverView;

-(void)insertLogoArtIntoTitle {
	
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	UINavigationController* nc;
	UIColor* myOrange		  = [UIColor colorWithRed:249.0/255.0 green:163.0/255.0 blue:14.0/255.0 alpha:1.0];
	
	for( UIViewController* vc in [myApp tabBarController].viewControllers ) {
		NSString* testString = (NSString*)vc.title;
		if ([testString isEqualToString:@"Favorites"]) {
			nc = (UINavigationController *)vc;
			nc.navigationBar.tintColor = myOrange;
			//[nc.navigationBar addSubview:label];
			break;
		}
	}

	return;
	
	UIImage* graphic		  = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"banner" ofType:@"png"]];
	UIImageView *showImage    = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 43.0)];
    showImage.opaque          = NO;    
	showImage.image           = graphic;
	
	[myNavBar addSubview:showImage];
	[graphic release];
	[showImage release];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	//cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];

    [super viewDidLoad];
	[self insertLogoArtIntoTitle];

    hoverView.center = [self view].center;
	
	[[self view] addSubview:hoverView];
    [[self view] bringSubviewToFront:hoverView];

	[self showHoverView:YES];
}

- (void)showHoverView:(BOOL)show
{
	// fade animate the view out of view by affecting its alpha
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.40];
    
	if (show)
	{
		hoverView.alpha = 0.85;
	}
	else
	{
		hoverView.alpha = 0.0;
	}
	
	[UIView commitAnimations];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

#pragma mark ---- Table View ----

// Number of table sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

// Return rows in table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];

	if ([myApp.favorites count] == 0) {[self showHoverView:NO];}

	return [myApp.favorites count];
}

// Determine whether to display cell
-(BOOL)displayMe:(int)row {
	
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	
	//if ([myApp.contacts count] == 0) {return false;}
	
	Contact *contact = (Contact *)[myApp.contacts objectAtIndex:row];
	
	BOOL retCode = [myApp.database isContactAFavorite:myApp.userString cid:contact.contactIdentifier];
		
	NSLog(@"%@ %@: %@ %@ %d", contact.givenName, contact.familyName, myApp.userString, contact.contactIdentifier, retCode);
		
	//return retCode;	
	return true;
}

// Set row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	if (![self displayMe:indexPath.row]) {return 0.0;}
    return 53.0;
}

// Reload favorites table
-(void)reloadData {
	[self.favoritesTable reloadData];
}

// Draw cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
    
	UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
    [cell backgroundView].opaque=YES;
    [cell backgroundView].clearsContextBeforeDrawing;
   	[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
	
	// See if we can display this row
	if (![self displayMe:indexPath.row]) {return cell;}
	
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
	// BoxView* box       = [[BoxView alloc] initWithFrame:CGRectMake(3.0, 3.0, 314.0, 50.0)];
	UIColor* textColor = [UIColor colorWithRed:85.0/255.0 green:85.0/255.0 blue:85.0/255.0 alpha:1.0];
	
	//[cell addSubview:box];
	
	// Dereference our contact
	Contact *contact=nil; 
	for (int x=0; x<[myApp.contacts count]; x++) {
		Contact *localContact = (Contact*)[myApp.contacts objectAtIndex:x]; 
		if ([localContact.contactIdentifier isEqualToString:[myApp.favorites objectAtIndex:indexPath.row]]) {
			contact = localContact;
			break;
			}
		}
	
	// Safety valve
	if (contact == nil) {return cell;}
	
	NSData* imageData         = contact.photoBlob;
	UIImage * graphic         = (UIImage*)[[UIImage alloc] initWithData:imageData];
	
	UIImageView *showImage    = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, ((54-(40 * graphic.size.height / graphic.size.width))/2.0), 40.0, 40.0 * (graphic.size.height / graphic.size.width))];
    showImage.opaque          = NO;    
	showImage.image           = graphic;
	
	// Fonts for Labels
	UIFont* teeny_font      = [UIFont systemFontOfSize:14];
	UIFont* bold_large_font = [UIFont boldSystemFontOfSize:18];
	
	// Allocate labels
	UILabel *nameLabel        = [[UILabel alloc] initWithFrame:CGRectMake(60.0, 7.0, 200.0, 20.0)];
	UILabel *jobLabel         = [[UILabel alloc] initWithFrame:CGRectMake(60.0, 27.0, 200.0, 20.0)];
	
	// Build Name Label
	nameLabel.text			  = [@"" stringByAppendingFormat:@"%@ %@", contact.givenName, contact.familyName];
	nameLabel.font            = bold_large_font;
	
	UIColor* myBGColor        = [UIColor colorWithRed:248.0/255.0 green:248.0/255.0 blue:248.0/255.0 alpha:1.0];
	
	nameLabel.backgroundColor        = [UIColor clearColor];
	jobLabel.backgroundColor         = [UIColor clearColor];
	cell.contentView.backgroundColor = myBGColor;
	
	// Set text color
	nameLabel.textColor       = textColor;
	jobLabel.textColor        = textColor;
	cell.textColor            = textColor;
	
	// Set label in cell
	[cell addSubview:nameLabel];
	
	// Do we have a company?
	if (contact.company) {
		// Build company label
		jobLabel.text		  = contact.company;
		jobLabel.font         = teeny_font;
		
		// Set label in cell
		[cell addSubview:jobLabel];
	}
	
	showImage.backgroundColor  = myBGColor;
	// Add image to cell
	[cell addSubview:showImage];
	
	// Cleanup our junk
	// [box release];
	[showImage release];
	[graphic release];
	[nameLabel release];
	[jobLabel release];
	
	// if we are on row zero, we are done and should hide the loading screen
	if (indexPath.row == 0) {
		[self showHoverView:NO];    
	}
	
	return cell;
}	

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	
	UITableViewCell *hitCell = [tableView cellForRowAtIndexPath:indexPath];	
    [hitCell setSelected:NO animated:YES];
	
	[myApp slideFavoriteIn:indexPath.row];
}

- (void)dealloc {
	[hoverView release];
	[favoritesTable release];
	[myNavBar release];
    [super dealloc];
}


@end
